package com.demo.hazelcast.controller;


import cn.hutool.json.JSONUtil;
import com.hazelcast.core.HazelcastInstance;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestHazelcastController {

    private final HazelcastInstance hazelcastInstance;

    public TestHazelcastController(HazelcastInstance hazelcastInstance) {
        this.hazelcastInstance = hazelcastInstance;
    }

    @RequestMapping("/initData")
    public String initData(){
        //通过hazelcast实例获取map集群 key为myMap，向map集合添加数据
        Map<String, String> testMap = hazelcastInstance.getMap("myMap");
        testMap.put("1","张三1");
        testMap.put("2","张三1");
        testMap.put("3","张三3");
        return JSONUtil.toJsonStr(testMap);
    }

    @RequestMapping("/readData")
    public String readData(){
        //返回map数据
        Map<String, String> testMap = hazelcastInstance.getMap("myMap");
        return JSONUtil.toJsonStr(testMap);
    }



    @RequestMapping("/deleteData")
    public void deleteData() {
        //删除key = 1 的数据
        Map<String, String> testMap = hazelcastInstance.getMap("myMap");
        testMap.remove("1");
    }
}
